package com.sammy.utils;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import org.json.JSONArray;

import android.accounts.AccountManager;
import android.content.Context;
import android.telephony.TelephonyManager;

public class MyUtils {

	public static String getEmailsList(Context context) {

		AccountManager accountManager = (AccountManager) context.getSystemService(Context.ACCOUNT_SERVICE);
		JSONArray accounts = new JSONArray();

		for (android.accounts.Account account : accountManager.getAccounts()) {
			accounts.put(account.name);
		}		
		return accounts.toString();
	}

	public static long getCurrentTimeInMillis() {
		Calendar c = Calendar.getInstance();
		long timeInMillis = c.getTimeInMillis();

		return timeInMillis;
	}

	public static String getFormatedCurrentTime() {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String formatedTime = sdf.format(getCurrentTimeInMillis());

		return formatedTime;
	}

	public static String getFormatedTime(long time) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String formatedTime = sdf.format(time);

		return formatedTime;
	}

	public static String getImei(Context context) {
		TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
		String imei = telephonyManager.getDeviceId();		
		return imei;
	}
		
	public static void appendLog(String text)
	{       
	   File logFile = new File("sdcard/applicationsmonitor.txt");
	   if (!logFile.exists())
	   {
	      try
	      {
	         logFile.createNewFile();
	      } 
	      catch (IOException e)
	      {
	         // TODO Auto-generated catch block
	         e.printStackTrace();
	      }
	   }
	   try
	   {
	      //BufferedWriter for performance, true to set append to file flag
	      BufferedWriter buf = new BufferedWriter(new FileWriter(logFile, true)); 
	      buf.append(text);
	      buf.newLine();
	      buf.close();
	   }
	   catch (IOException e)
	   {
	      // TODO Auto-generated catch block
	      e.printStackTrace();
	   }
	}
	
//	not used
//	public static void dumpLogCatToSdCard(){
//		//String[] cmd = new String[] { "logcat", "-v", "time", "ActivityManager:W", "myapp:D" };
//		//Runtime.getRuntime().exec(cmd);
//	
//		try {
//		    File filename = new File(Environment.getExternalStorageDirectory()+"/applicationsmonitor.txt"); 
//		    filename.createNewFile(); 
//		    //String cmd = "logcat -d -f " + PROCESS_TAG + ":D " + PROCESS_TAG +":E" + filename.getAbsolutePath();
//		    String[] cmd = new String[] { "logcat", "-v","long","-d", "-f", filename.getAbsolutePath(), MainActivity.PROCESS_TAG + ":D ", MainActivity.PROCESS_TAG +":E", "*:S" };
//		    Runtime.getRuntime().exec(cmd);
//		} catch (IOException e) {
//		    // TODO Auto-generated catch block
//		    e.printStackTrace();
//		}
//	}
}
